package com.tanhua.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.Log;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface LogMapper extends BaseMapper<Log> {

    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE TYPE=#{type} AND log_time=#{logTime}")
    Integer queryByTypeAndLogTime(@Param("type") String type, @Param("logTime") String logTime); //根据操作时间和类型


    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE log_time=#{logTime}")
    Integer queryByLogTime(String logTime); //展示记录时间查询


    @Select("SELECT COUNT(DISTINCT user_id)  FROM tb_log WHERE log_time=#{today} AND user_id IN (\n " +
            " SELECT user_id FROM tb_log WHERE TYPE=\"0102\" AND log_time=#{yestoday} \n " +
            ")")
    Integer queryNumRetention1d(@Param("today") String today,@Param("yestoday") String yestoday); //查询次日留存

    //获取过去七天或者三十天内活跃用户
    @Select("SELECT count(DISTINCT user_id) FROM tb_log where DATE_SUB(CURDATE(), INTERVAL #{days} DAY) <= date(log_time)")
    Integer activePassMonthOrWeek(@Param("days") int days);

    //今日登录次数
    @Select("select count(DISTINCT log.user_id) from (select * from tb_log where type = #{type}) `log` where log.log_time = #{format}")
    Integer loginTimesToday(@Param("type") String type, @Param("format") String format);

    /**
     * type != 0101 用户登录不代表就是活跃用户
     * @param format 当前日期或昨日日期 格式类型为 yyyy-MM-dd
     * @return
     */
    @Select("select count(DISTINCT log.user_id) from (select * from tb_log where type != '0101') `log` where log.log_time = #{format}")
    Integer activeUsersToday(String format);
}
